package com.mediacloud.app.newsmodule.view.share

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.mediacloud.app.assembly.util.QRCodeUtil
import com.mediacloud.app.assembly.util.buildUrl
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*

/**
 * 微直播的海报分享
 */
class PosterSharePop2(context: Context) : AbsPosterSharePop(context) {

    val posterRootView: View
    val posterView: View
    val allView: View
    val logoIv: ImageView
    val titleTv: TextView
    val startTimeTv: TextView
    val hitCountTv: TextView
    val qrCodeIv: ImageView
    val container: LinearLayout

    init {
        val lp = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)
        lp.addRule(RelativeLayout.CENTER_IN_PARENT)
        posterRootView = LayoutInflater.from(context).inflate(R.layout.share_poster2, null, false)
        posterView = posterRootView.findViewById(R.id.posterView)
        container = posterRootView.findViewById(R.id.container)
        logoIv = posterRootView.findViewById(R.id.logoIv)
        titleTv = posterRootView.findViewById(R.id.titleTv)
        startTimeTv = posterRootView.findViewById(R.id.startTimeTv)
        hitCountTv = posterRootView.findViewById(R.id.hitCountTv)
        qrCodeIv = posterRootView.findViewById(R.id.qrCodeIv)
        allView = posterRootView.findViewById(R.id.allView)
        posterContainer.addView(posterRootView, lp)
        container.setOnClickListener {
            dismiss()
        }
    }

    override fun posterView(): View {
        return allView
    }

    @SuppressLint("SetTextI18n")
    override fun updateData(article: ArticleItem, bitmap: Bitmap?) {
        posterContainer.visibility = View.VISIBLE
        if (bitmap != null) {
            logoIv.setImageBitmap(bitmap)
            logoIv.visibility = View.VISIBLE
        } else {
            loadX(context, buildUrl(3, article.logo), 1, object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    return true
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    logoIv.setImageDrawable(resource)
                    logoIv.visibility = View.VISIBLE
                    return true
                }
            })
        }
        if (!TextUtils.isEmpty(article.shareTitle)) {
            titleTv.text = article.shareTitle
        } else {
            titleTv.text = article.title
        }
        val qrCode = QRCodeUtil.createQRImage(article.url, qrCodeIv.width, qrCodeIv.height, null)
        qrCodeIv.setImageBitmap(qrCode)
        article.orginDataObject?.optJSONObject("AppCustomParams")
            ?.optJSONObject("movie")?.apply {
                val string2Millis = TimeUtils.string2Millis(optString("startTime"))
                val simpleDateFormat = SimpleDateFormat("yyyy年MM月dd日 HH:mm")
                startTimeTv.text =
                    TimeUtils.millis2String(string2Millis, simpleDateFormat.format(string2Millis))
            }
        if (article.hitCount == 0L) {
            hitCountTv.visibility = View.INVISIBLE
        } else {
            hitCountTv.visibility = View.VISIBLE
            val decimalFormat = DecimalFormat("00000000")
            val numFormat: String = decimalFormat.format(article.hitCount)
            hitCountTv.text = "NO.$numFormat"
        }
    }
}